part of AppUtils;

extension Account on _AppUtils {
  void send_sms_server(YPage page, String phone, onSuccess()) {
    if (phone.isEmpty) {
      yToast("请输入手机号");
      return;
    }
    page.yPost(IFinal.Jkids.getCode, {
      "phone": phone,
    }, (isSuccess, results, code, message) {
      if (isSuccess) {
        yToast("验证码发送成功，请注意查收");
        onSuccess();
      }
    });
  }

  Future<void> check_sms_server(YPage page, String phone, String code, onSuccess(UserModel userModel)) async {
    if (phone.isEmpty) {
      yToast("请输入手机号");
      return;
    }
    if (code.isEmpty) {
      yToast("请输入短信验证码");
      return;
    }
    page.yPost<UserModel>((await checkIsLogon(page)) ? IFinal.Jkids.bind_phone : IFinal.Jkids.login_phone, {
      "phone": phone,
      "code": code,
      "type": "server",
    }, (isSuccess, results, code, message) {
      if (isSuccess) onSuccess(results!.first);
    }, onModel: (map) => UserModel.fromJson(map));
  }

  void send_sms_sdk(String phone, onSuccess()) {
    if (phone.isEmpty) {
      yToast("请输入手机号");
      return;
    }
    yShowActionSheet("请选择验证方式", ["短信", "语音"], (index, action) {
      switch (action) {
        case "短信":
          yShowProgress();
          Smssdk.getTextCode(phone, "86", "", (ret, err) {
            yDismissProgress();
            if (err != null) {
              yToast("验证码发送失败，请重试，$err");
              return;
            }
            yToast("验证码发送成功，请注意查收");
            onSuccess();
          });
          break;
        case "语音":
          yShowProgress();
          Smssdk.getVoiceCode(phone, "86", (ret, err) {
            yDismissProgress();
            if (err != null) {
              yToast("验证码发送失败，请重试，$err");
              return;
            }
            yToast("验证码发送成功，请注意查收");
            onSuccess();
          });
          break;
      }
    });
  }

  void check_sms_sdk(YPage page, String phone, String code, onSuccess(UserModel userModel)) {
    if (phone.isEmpty) {
      yToast("请输入手机号");
      return;
    }
    if (code.isEmpty) {
      yToast("请输入短信验证码");
      return;
    }
    yShowProgress();
    Smssdk.commitCode(phone, "86", code, (ret, err) async {
      yDismissProgress();
      if (err != null) {
        yToast("验证码校验失败，请重试，$err");
        return;
      }
      page.yPost<UserModel>((await checkIsLogon(page)) ? IFinal.Jkids.bind_phone : IFinal.Jkids.login_phone, {
        "phone": phone,
        "code": code,
        "type": "sdk",
      }, (isSuccess, results, code, message) {
        if (isSuccess) onSuccess(results!.first);
      }, onModel: (map) => UserModel.fromJson(map));
    });
  }

  Future<void> check_account(YPage page, String password, {String? loginname, String? code, String? password_new, onSuccess(UserModel userModel)?, onFail()?}) async {
    if (password.isEmpty) {
      yToast("请输入密码");
      return;
    }

    page.yPost<UserModel>((await checkIsLogon(page)) ? IFinal.Jkids.bind_account : IFinal.Jkids.login_account, {
      "loginname": loginname,
      "password": EncodeUtils.encodePassword(password),
      "password_new": EncodeUtils.encodePassword(password_new),
      "code": code,
    }, (isSuccess, results, code, message) {
      if (isSuccess)
        onSuccess?.call(results!.first);
      else
        onFail?.call();
    }, onModel: (map) => UserModel.fromJson(map));
  }

  void check_verify(YPage page, onSuccess(UserModel userModel)) {
    Future<void> doResult(Map? err, Map? result) async {
      if (err != null) {
        yToast("一键登录失败，请重试，$err");
        return;
      }
      page.yPost<UserModel>((await checkIsLogon(page)) ? IFinal.Jkids.bind_verify : IFinal.Jkids.login_verify, {
        "token": result?["token"],
        "opToken": result?["opToken"],
        "operator": result?["operator"],
      }, (isSuccess, results, code, message) {
        if (isSuccess) onSuccess(results!.first);
      }, onModel: (map) => UserModel.fromJson(map));
    }

    yShowProgress();
    // Secverify.preVerify(result: (ret, err) {
    //   yDismissProgress();
    //   if (err != null) {
    //     yToast("一键登录初始化失败，请重试，$err");
    //     return;
    //   }
    //   Secverify.autoFinishOAuthPage(flag: true);
    //   final config = SecVerifyUIConfig();
    //   config.iOSConfig!.manualDismiss = false;
    //
    //   Secverify.verify(config, (rt, err) {
    //     //IOS拉起授权页结果回调
    //   }, (rt, err) {
    //     //IOS退出一键登录页面回调
    //   }, (rt, err) {
    //     //IOS一键登录回调
    //     doResult(err, rt);
    //   }, (rt, err) {
    //     //IOS自定义回调回调
    //   }, (rt, err) {
    //     //安卓登录回调
    //     doResult(err, rt?["ret"]);
    //   });
    // });
  }

  void check_third(YPage page, ShareSDKPlatform? thirdPlatform, onSuccess(UserModel userModel)) {
    if (thirdPlatform == null) {
      yToast("平台类型错误");
      return;
    }
    yShowProgress();
    SharesdkPlugin.getUserInfo(thirdPlatform, (state, userInfo, error) async {
      yDismissProgress();
      if (state != SSDKResponseState.Success) {
        if (error.rawData != null) yToast("三方登录失败：${error.rawData}");
        return;
      }
      page.yPost<UserModel>((await checkIsLogon(page)) ? IFinal.Jkids.bind_third : IFinal.Jkids.login_third, {
        "platform": thirdPlatform.name,
        "thirdID": userInfo["userId"],
      }, (isSuccess, results, code, message) {
        if (isSuccess) onSuccess(results!.first);
      }, onModel: (map) => UserModel.fromJson(map));
    });
  }
}
